
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { homeRecommendDelete ,homeRecommendCreate, homeRecommendCreateone, homeRecommendCreatetow, getHomeRecommendList,getEstateList,getActivityList,getHomeRecommendActivityList,getHomeRecommendNewsList
,getNewsList,saveIsLive,getIsLive, regionArea, target, PUTLOUP,PUTTUI,PUTZHIX} from './indexSettingListServ.js'

const defultState = {
    loading:false,
    searchlist: [],
    quer: {
        status: true,
        resourceType: 1,
        provinceCode: null,
        cityCode: null,
    },
    quers: {},
    isLive:0,//是否显示直播信息
    //推荐楼盘
    options:[],
    queryData:{},
    buildData:{
      tableData:{
          dataSource:[]
      },
      paginationData:{
          pageSize:10,
          pageNo:1,
      },
      paginationModalData:{},
      modalTableData:{
          dataSource:[]
        },
      modal_visible:false,
      conditionDetail:{
          productName:"",
          productCode:"",
      }
    },

    //推荐活动
    activityData:{
        tableData:{
            dataSource:[]
        },
        paginationData:{
            pageSize:10,
            pageNo:1,
        },
        paginationModalData:{},
        modalTableData:{
            dataSource:[]
        },
        modal_visible:false,
        conditionDetail:{
            activityName:"",
            productName:"",
        }
    },
    //推荐资讯
    informationData:{
        tableData:{
            dataSource:[]
        },
        paginationData:{
            pageSize:10,
            pageNo:1,
        },
        paginationModalData:{},
        modalTableData:{
            dataSource:[]
        },
        modal_visible:false,
        conditionDetail:{
            newsName:"",
            productName:"",
        }
    },

    type:'build',


}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'queryList', payload: {}})
          dispatch({type: 'queryActivityList', payload: {}})
          dispatch({type: 'queryNewsList', payload: {}})
          dispatch({type:'getIsLive',payload: {}})
          dispatch({type:'regionArea',payload: {}})
        }
      })
    },
  },


  effects: {
      // 查询城市
    *regionArea({payload},{put,call,select}){
        let {options}=yield select(state => state[tmpModule.namespace])
        const {success,data}=yield call(regionArea)
        if(success){
            options = data ? data.map(i =>{
                return {
                    label: i.label,
                    value: i.value,
                    children: i.children ? i.children.map(j => {
                        return {
                            label: j.label,
                            value: j.value,
                        }
                    }) : []
                }
            }) : []
        yield put({type:'updateStore',payload:{options}})
        }
    },
    // 查询弹窗下拉
    *target({resourceType,provinceCode,cityCode,searchName},{put,call,select}){
        let {searchlist,quer}=yield select(state => state[tmpModule.namespace]) 
        const {success,rows}=yield call(target,quer.resourceType,quer.provinceCode,quer.cityCode,quer.searchName)
        if(success){
            searchlist = rows
        yield put({type:'updateStore',payload:{searchlist}})
        console.log(searchlist,11111111111)
        }
    },
      //推荐楼盘列表
    *queryList({ payload }, { put, call, select }) {

      try{
        let {buildData, queryData} = yield select(state => state[tmpModule.namespace])
        let result  = yield call(getHomeRecommendList, parse({
            ...queryData,
            pageSize:payload.pageSize||10,
            pageNo:payload.pageNo || buildData.paginationData.pageNo
        }))
          if(result.success){
              console.log(result.rows,2222)
              result.rows.forEach((item,index)=>{
                item['key'] = index+1
                })
              buildData.tableData.dataSource = result.rows
              buildData.paginationData = result,
              yield put({type: 'updateStore', payload:{buildData}})
          }
      }catch(e){
        console.error(e)
      }
    },
      //推荐活动列表
    *queryActivityList({ payload }, { put, call, select }) {
          try{
              let {activityData,queryData} = yield select(state => state[tmpModule.namespace])
              let result  = yield call(getHomeRecommendActivityList, parse({
                  ...queryData,
                  pageSize:payload.pageSize||10,
                  pageNo:payload.pageNo || activityData.paginationData.pageNo
              }))
              if(result.success){
                  result.rows.forEach((item,index)=>{
                      item['key'] = index+1
                  })
                  activityData.tableData.dataSource = result.rows
                  activityData.paginationData = result,
                      yield put({type: 'updateStore', payload:{activityData}})
              }
          }catch(e){
              console.error(e)
          }
      },
      //推荐资讯列表
    *queryNewsList({ payload }, { put, call, select }) {
          try{
              let {informationData,queryData} = yield select(state => state[tmpModule.namespace])
              let result  = yield call(getHomeRecommendNewsList, parse({
                  ...queryData,
                  pageSize:payload.pageSize||10,
                  pageNo:payload.pageNo || informationData.paginationData.pageNo
              }))
              if(result.success){
                  result.rows.forEach((item,index)=>{
                      item['key'] = index+1
                  })
                  informationData.tableData.dataSource = result.rows
                  informationData.paginationData = result,
                   yield put({type: 'updateStore', payload:{informationData}})
              }
          }catch(e){
              console.error(e)
          }
      },
      // 获取推荐楼盘搜索列表
    *estateList({ payload }, { put, call, select }) {
        try{
            let {buildData} = yield select(state => state[tmpModule.namespace])
          yield put({ type: 'updateStore', payload: { loading: true,btnLoading: true, } })
            let result  = yield call(getEstateList, parse({
                productName:buildData.conditionDetail.productName,
                productCode:buildData.conditionDetail.productCode,
                pageSize:payload.pageSize||10,
                pageNo:payload.pageNo||buildData.paginationData.pageNo
              }))
          yield put({ type: 'updateStore', payload: { loading: false,btnLoading: false, } })
              if(result.success){
                  result.rows.forEach((item,index)=>{
                      item['key'] = index+1
                  })
                  buildData.paginationModalData = result
                  buildData.modalTableData.dataSource = result.rows
                  yield put({type: 'updateStore', payload:{buildData}})
              }
          }catch(e){
              console.error(e)
          }
      },

      // 获取推荐活动搜索列表
    *activityList({ payload }, { put, call, select }) {
          try{
              let {activityData} = yield select(state => state[tmpModule.namespace])
            yield put({ type: 'updateStore', payload: { loading: true,btnLoading: true, } })
              let result  = yield call(getActivityList, parse({
                  productName:activityData.conditionDetail.productName,
                  activityName:activityData.conditionDetail.activityName,
                  pageSize:payload.pageSize||10,
                  pageNo:payload.pageNo||1
              }))
            yield put({ type: 'updateStore', payload: { loading: false,btnLoading: false, } })
              if(result.success){
                  result.rows.forEach((item,index)=>{
                      item['key'] = index+1
                  })
                  activityData.paginationModalData = result
                  activityData.modalTableData.dataSource = result.rows
                  yield put({type: 'updateStore', payload:{activityData}})
              }
          }catch(e){
              console.error(e)
          }
      },
    *getIsLive({ payload }, { put, call, select }){
        const { data,success } = yield call(getIsLive,{homeRecommendId:0})
        if(data){
            yield put({ type: 'updateStore', payload: { isLive:data.isLive} })
        }
    },
    *saveIsLive({ payload }, { put, call, select }){
        const { success }  = yield call(saveIsLive, parse({
            homeRecommendId:0,
            isLive:payload.isLive
        }))
        if(success){
            yield put({ type: 'updateStore', payload: { isLive:payload.isLive} })
        }
    },
      // 获取推荐资讯搜索列表
    *newsList({ payload }, { put, call, select }) {
          try{
              let {informationData} = yield select(state => state[tmpModule.namespace])
            yield put({ type: 'updateStore', payload: { loading: true,btnLoading: true, } })
              let result  = yield call(getNewsList, parse({
                  productName:informationData.conditionDetail.productName||'',
                  newsName:informationData.conditionDetail.newsName||'',
                  pageSize:payload.pageSize||10,
                  pageNo:payload.pageNo||1
              }))
            yield put({ type: 'updateStore', payload: { loading: false,btnLoading: false, } })
              if(result.success){
                  result.rows.forEach((item,index)=>{
                      item['key'] = index+1
                  })
                  informationData.paginationModalData = result
                  informationData.modalTableData.dataSource = result.rows
                  yield put({type: 'updateStore', payload:{informationData}})
              }
          }catch(e){
              console.error(e)
          }
      },
      //新增推荐楼盘
      *create({ payload }, { put, call, select }) {
          try{
          let {quer,buildData} = yield select(state => state[tmpModule.namespace])
          let obj = {
            resourceId: quer.resourceId,
            validityEndTime: quer.validityEndTime,
            validityStartTime: quer.validityStartTime,
            productTag: quer.productTag
          }
          const { success } = yield call(homeRecommendCreate, obj)
              if(success){
                buildData.modal_visible = false
                yield put({type: 'updateStore', payload:{buildData}})
                yield put({type: 'queryList', payload:{}})
              }
          }catch(e){
              console.error(e)
          }
      },
      //新增推荐
      *createone({ payload }, { put, call, select }) {
        try{
        let {quer,activityData} = yield select(state => state[tmpModule.namespace])
        let obj = {
          resourceId: quer.resourceId,
          validityEndTime: quer.validityEndTime,
          validityStartTime: quer.validityStartTime,
        }
        const { success } = yield call(homeRecommendCreateone, obj)
            if(success){
                activityData.modal_visible = false
              yield put({type: 'updateStore', payload:{activityData}})
              yield put({type: 'queryActivityList', payload:{}})
            }
        }catch(e){
            console.error(e)
            }
        },
        //新增资讯
        *createtow({ payload }, { put, call, select }) {
            try{
            let {quer,informationData} = yield select(state => state[tmpModule.namespace])
            let obj = {
              resourceId: quer.resourceId,
              validityEndTime: quer.validityEndTime,
              validityStartTime: quer.validityStartTime,
            }
            const { success } = yield call(homeRecommendCreatetow, obj)
                if(success){
                    informationData.modal_visible = false
                  yield put({type: 'updateStore', payload:{informationData}})
                  yield put({type: 'queryNewsList', payload:{}})
                }
            }catch(e){
                console.error(e)
            }
        },
        //更新
        *uploup({ payload }, { put, call, select }) {
            try{
            let {quer,buildData} = yield select(state => state[tmpModule.namespace])
            let obj = {
                homeRecommendId: quer.homeRecommendId,
                validityEndTime: quer.validityEndTime,
                validityStartTime: quer.validityStartTime,
                productTag: quer.productTag
            }
            const { success } = yield call(PUTLOUP, obj)
                if(success){
                  buildData.modal_visible = false
                  yield put({type: 'updateStore', payload:{buildData}})
                  yield put({type: 'queryList', payload:{}})
                }
            }catch(e){
                console.error(e)
            }
        },
        *uphuod({ payload }, { put, call, select }) {
            try{
            let {quer,activityData} = yield select(state => state[tmpModule.namespace])
            let obj = {
                homeRecommendId: quer.homeRecommendId,
                validityEndTime: quer.validityEndTime,
                validityStartTime: quer.validityStartTime,
            }
            const { success } = yield call(PUTTUI, obj)
                if(success){
                    activityData.modal_visible = false
                  yield put({type: 'updateStore', payload:{activityData}})
                  yield put({type: 'queryActivityList', payload:{}})
                }
            }catch(e){
                console.error(e)
            }
        },
        *upzhixun({ payload }, { put, call, select }) {
            try{
            let {quer,informationData} = yield select(state => state[tmpModule.namespace])
            let obj = {
                homeRecommendId: quer.homeRecommendId,
                validityEndTime: quer.validityEndTime,
                validityStartTime: quer.validityStartTime,
            }
            const { success } = yield call(PUTZHIX, obj)
                if(success){
                    informationData.modal_visible = false
                  yield put({type: 'updateStore', payload:{informationData}})
                  yield put({type: 'queryNewsList', payload:{}})
                }
            }catch(e){
                console.error(e)
            }
        },
      //删除推荐楼盘
      *homeRecommendOfDelete({ payload }, { put, call, select }) {
          try{
              let obj = {
                homeRecommendId: payload.item.homeRecommendId,
                resourceType: payload.type === 'build' ? 1 : payload.type === 'activity' ? 2:3
              }
              const {msg, data , success } = yield call(homeRecommendDelete, obj)
            if(success){
                message.success('置顶成功');
                if(payload.type === 'build') {
                yield put({type: 'queryList', payload:{}})
                } else if(payload.type === 'activity'){
                yield put({type: 'queryActivityList', payload:{}})
                }else {
                yield put({type: 'queryNewsList', payload:{}})
                } 
            }
          }catch(e){
              console.error(e)
          }
      },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
